#!/usr/bin/perl -w


	use LWP 5.64;
	use HTTP::Cookies; 
	use HTTP::Request::Common;
	use URI::Escape;
	
	my $use_proxy = 'http://127.0.0.1:8118/'; # proxy address here
	
	my $login_mail = 'mail';
	my $login_pass = 'pass';
	# URL we want to post to
	my $url_post = 'http://vkontakte.ru/';
	
	my $msg_file = '~/.quotes.txt';
	
	# Quotes delimiter. 
	# To separate qoutes with ONE new line: $delim = "\r\n"; (or "\n" in UNIX EOL mode)
	# To separate qoutes with TWO new lines: $delim = "\r\n\r\n"; (or "\n\n" in UNIX EOL mode)
	# To separate qoutes with any symbol: $delim = "_symbol_";
	# Note that you must put "\"  before Perl's special symbols: ", ', (, ), [, ], and others
	my $delim = "\r\n\r\n";
	my $repeat = 1000; # repeat count
	my $delay = 4; # 4 is minimum delay for walls,albums and boards.  5 - for mail
	my $postfix = 1; # set 1 if you want to post many equal messages
	
	my $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1;) Firefox/2.0.0.0';
	my $url_login = 'http://vkontakte.ru/login.php';
	
	my $ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => [ 'POST', 'GET' ]);
	$ua->proxy('http', $use_proxy) if $use_proxy;
	$ua->agent($useragent);
	$ua->cookie_jar({});
	
	print 'Login... ';
	my $response = $ua->post($url_login, 
		[	'email' => $login_mail, 
			'pass' => $login_pass, 
		]);
	die 'Login error -- ', $response->status_line
		unless $response->is_success or $response->code == 302;
	print "Success.\n";
	srand;
	
	print 'Load quotes... ';
	my $buff;
	my $filedata;
	open(MSG, $msg_file) or die "open error!";;
	binmode MSG;
	while (read(MSG, $buff, 1024)) { $filedata .= $buff; }
	@messages = split $delim, $filedata;
	print "Success...\n";
	
	print 'Get URL content... ';
	$response = $ua->get($url_post);
	die 'Get page error -- ', $response->status_line
		unless $response->is_success or $response->code == 302;
	print "Success.\n";
	my $cont = $response->content;
	my $url = $url_post;
	my $res; my $app='';

L0:	foreach $message (@messages) {	
		# add random number to the end of the message
		$app = int(rand(999999999)) if $postfix;
		
		$_ = $cont;	
		if ($cont =~ /action\s*=\s*\"wall.php\"\s*name\s*=\s*\"postMessage\"\s*id\s*=\s*\"postMessage\"/) {
			($res) = /makeFRequest\s*\(\s*\'\s*wall\.php\?([^\?]+)\'\s*,\s*3\s*\)/;
			$url = 'http://vkontakte.ru/wall.php?'."$res";
			($id_value) = /name\s*=\s*\"to_id\"\s*value\s*=\s*\"(\w+)\"/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						to_id => $id_value,
						act => 'sent',
						message => "$message"."\n$app"
					];
		} elsif ($cont =~ /id\s*=\s*\"comment\"\s*name\s*=\s*\"comment\"\s*action\s*=\s*\'photos.php\'/) {
			$url = 'http://vkontakte.ru/photos.php';
			($id_value) = /name\s*=\s*\"id\"\s*value\s*=\s*\"([^\"]+)\"/;
			($aid) = /name\s*=\s*\"aid\"\s*value\s*=\s*\"(\d+)\"/;

			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						id => $id_value,
						aid => $aid,
						act => 'post',
						comment => "$message"."\n$app",
						add_bookmark => '1'
					];
		} elsif ($cont =~ /action\s*=\s*\"board.php\"\s*name\s*=\s*\"editPost\"\s*id\s*=\s*\"editPost\"/) {
			($id_value) = /name\s*=\s*\"id\"\s*value\s*=\s*\"(\d+)\"/;
			$url = 'http://vkontakte.ru/board.php?act=newtopic&id='.$id_value;
			($hash) = /name\s*=\s*\"hash\"\s*value\s*=\s*\"(\w+)\"/;
			print "$url\n$hash\n";
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						act => 'do_new_topic',
						id => $id_value,
						hash => $hash,
						title => substr($message,0,32),
						post => $message
					];
		} elsif ($cont =~ /id\s*=\s*\"postmessage\"\s*name\s*=\s*\"postmessage\"\s*action\s*=\s*\'board.php\'/) {
			$url = 'http://vkontakte.ru/board.php';
			($id_value) = /name\s*=\s*\"tid\"\s*value\s*=\s*\"(\d+)\"/;
			($gid) = /name\s*=\s*\"gid\"\s*value\s*=\s*\"(\d+)\"/;
			
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						tid => $id_value,
						gid => $gid,
						act => 'do_add_post',
						post => "$message"."\n$app",
						add_bookmark => '1'
					];
		} elsif ($cont =~ /action\s*=\s*\"mail.php\"\s*name\s*=\s*\"postMessage\"\s*id\s*=\s*\"postMessage\"/) {
			$url = 'http://vkontakte.ru/mail.php';
			($id_secure) = /name\s*=\s*\"secure\"\s*value\s*=\s*\"(\w+)\"/;
			($id_photo) = /name\s*=\s*\"photo\"\s*value\s*=\s*\"(\w+)\"/;
			($id_value) = /name\s*=\s*\"to_id\"\s*value\s*=\s*\"(\d+)\"/;
			$req = POST $url, Content_Type => 'application/x-www-form-urlencoded',
					Content      => [
						secure => $id_secure,
						photo => $id_photo,
						to_id => $id_value,
						to_reply => '0',
						act => 'sent',
						title => substr($message,0,32),
						message => "$message"."\n$app"
					];
		}	
		
		print 'Send... ';
		$response = $ua->request($req);
		do { warn 'Post error -- ', $response->status_line; next; }
			unless $response->is_success or $response->code == 302;
		print "Success.\n";
		sleep $delay;
	}
	
	goto L0 if ($repeat--);